home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Cafe 3
/
Visual Cafe 3.ISO
/
Vcafe
/
Source.bin
/
RelativeURL.java
< prev
next >
Wrap
Text File
|
1998-08-21
|
4KB
|
119 lines
package symantec.itools.net;
import java.net.URL;
import java.net.MalformedURLException;
import symantec.itools.lang.Context;
import java.io.*;
// 05/15/97 CAR Added ieAnchorHack to work around an IE problem with anchor symbols in URLs.
/**
* This class is used in conjunction with class symantec.itools.OS.Context to
* provide URLs that are relative to an appletÆs or applicationÆs
* document base. For applets the document base is the URL of the document
* that the applet is embedded in. For applications the document base is the
* same as the ôuser.dirö system property.
*/
public class RelativeURL
{
/**
* DonÆt use, this is an all-static class.
*/
public RelativeURL() {
}
/**
* Determines the absolute URL given a relative URL.
* If the spec parameter is relative, it is considered to be relative
* to the current document base as determined by getDocumentBase() in
* class symantec.itools.lang.Context.
*
* @param spec a possibly relative URL
* @return the absolute URL equivalent to the given relativeURL
* @exception MalformedURLException
* if cannot generate the resultant URL due to a bad spec parameter
* or a bad document base
*/
public static URL getURL(String spec)
throws MalformedURLException
{
// InternetExplorer 3 for some reason strips out a single anchor symbol (#)
// when the URL is passed to showDocument() so we double up the symbol (##)
if (System.getProperty("java.vendor").startsWith("Microsoft") &&
System.getProperty("java.version").startsWith("1.0") &&
spec.indexOf('#') > -1) {
spec = ieAnchorHack(spec);
}
URL documentBase = Context.getDocumentBase();
if(documentBase != null && spec.indexOf("//") == -1)
{
return new URL(documentBase,spec);
}
return new URL(spec);
}
private static String ieAnchorHack(String spec) {
String str = spec.substring(0, spec.lastIndexOf('#'));
str += "#";
str += spec.substring(spec.lastIndexOf('#'));
return str;
}
/**
* Attempts to load an object from a .SER file.
*
* @param Class the class to be created
* @param serName the name of the serialization file
* @return the object created (or NULL, if any error occurred)
*/
public static Object loadObject(Class c, String serName) {
Object ro = null;
java.io.InputStream ins;
ClassLoader loader;
loader = c.getClassLoader();
if (loader == null)
ins = ClassLoader.getSystemResourceAsStream(serName);
else
ins = loader.getResourceAsStream(serName);
if (ins == null) {
try {
ins = new FileInputStream(serName);
} catch (Exception e) {
}
}
if (ins != null) {
try {
ObjectInputStream ois = new ObjectInputStream(ins);
ro = ois.readObject();
ois.close();
} catch (Exception e) {
}
try {
ins.close();
} catch (Exception e) {
}
}
if (ro == null) {
try {
ro = c.newInstance();
} catch (Exception e) {
}
}
return ro;
}
}